package com.me.jingdongshop.ui.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.me.common.base.BaseActivity;
import com.me.common.base.Const;
import com.me.common.base.Global;
import com.me.common.base.OttoBus;
import com.me.common.ui.EditLayout;
import com.me.common.util.SharedPreUtil;
import com.me.jingdongshop.R;
import com.me.jingdongshop.model.bean.LoginBean;
import com.me.jingdongshop.model.protocol.IHttpService;
import com.me.jingdongshop.presenter.TestFragmentPre;

/**
 * Created by CJC on 2017/5/5.
 */

public class LoginAndRegisterActivity extends BaseActivity{

    private LinearLayout llLoginRegister;
    private ImageButton loginRegisterBack;
    private LinearLayout llLogin;
    private TextView textViewLogin;
    private TextView textView2Login;
    private LinearLayout llRegister;
    private TextView textViewRegister;
    private TextView textView2Register;
    private EditLayout userNameLoginRegister;
    private EditText passWordLoginRegister;
    private TextView textViewOnPassWord2;
    private EditText passWord2LoginRegister;
    private Button btLoginRegister;
    private TextView loginForgetPassWord;
    private TextView textViewLoginRegisterSimple;
    private LinearLayout loginRegisterOnQQ;
    private LinearLayout loginRegisterOnWechat;

    private boolean isLogin = true;
    private String mUsername;
    private String mPassword;
    private String mPassword2;
    private TextView tvConfirmPassword;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getLayoutRes() {
        return R.layout.login_register_pager;
    }

    @Override
    public void initView() {
        tvConfirmPassword = (TextView) findViewById(R.id.tv_confirm_password);
        llLoginRegister = (LinearLayout) findViewById(R.id.ll_login_register);
        loginRegisterBack = (ImageButton) findViewById(R.id.login_register_back);
        llLogin = (LinearLayout) findViewById(R.id.ll_login);
        textViewLogin = (TextView) findViewById(R.id.textView_login);
        textView2Login = (TextView) findViewById(R.id.textView2_login);
        llRegister = (LinearLayout) findViewById(R.id.ll_register);
        textViewRegister = (TextView) findViewById(R.id.textView_register);
        textView2Register = (TextView) findViewById(R.id.textView2_register);
        userNameLoginRegister = (EditLayout) findViewById(R.id.userName_login_register);
        passWordLoginRegister = (EditText) findViewById(R.id.passWord_login_register);
        textViewOnPassWord2 = (TextView) findViewById(R.id.textView_on_passWord2);
        passWord2LoginRegister = (EditText) findViewById(R.id.passWord2_login_register);
        btLoginRegister = (Button) findViewById(R.id.bt_login_register);
        loginForgetPassWord = (TextView) findViewById(R.id.login_forget_passWord);
        textViewLoginRegisterSimple = (TextView) findViewById(R.id.textView_Login_register_simple);
        loginRegisterOnQQ = (LinearLayout) findViewById(R.id.login_register_on_QQ);
        loginRegisterOnWechat = (LinearLayout) findViewById(R.id.login_register_on_wechat);

        initLoginRegisterpager();
    }

    @Override
    public void initListener() {
        initLoginOrRegister();
        initLoginRegister();
        loginRegisterBack.setOnClickListener(this);
    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View v, int id) {
        if(v == loginRegisterBack){
            finish();
        }
    }

    private void initLoginRegisterpager() {
        if (isLogin) {
            textViewLogin.setTextColor(Color.parseColor("#FF8FB7"));
            textView2Login.setBackgroundColor(Color.parseColor("#FF8FB7"));
            textView2Login.setVisibility(View.VISIBLE);
            tvConfirmPassword.setVisibility(View.GONE);//隐藏确认密码
            textViewRegister.setTextColor(Color.parseColor("#C6C6C6"));
            textView2Register.setVisibility(View.GONE);
            textViewOnPassWord2.setVisibility(View.GONE);
            passWord2LoginRegister.setVisibility(View.GONE);
            btLoginRegister.setText("登 陆");
            textViewLoginRegisterSimple.setText("一键登陆");
            loginForgetPassWord.setVisibility(View.VISIBLE);
            userNameLoginRegister.setText("");
            passWordLoginRegister.setText("");
            passWord2LoginRegister.setText("");
        } else {
            textViewLogin.setTextColor(Color.parseColor("#C6C6C6"));
            textView2Login.setVisibility(View.GONE);
            tvConfirmPassword.setVisibility(View.VISIBLE);//显示确认密码
            textViewRegister.setTextColor(Color.parseColor("#FF8FB7"));
            textView2Register.setBackgroundColor(Color.parseColor("#FF8FB7"));
            textView2Register.setVisibility(View.VISIBLE);
            textViewOnPassWord2.setVisibility(View.VISIBLE);
            passWord2LoginRegister.setVisibility(View.VISIBLE);
            btLoginRegister.setText("注 册");
            textViewLoginRegisterSimple.setText("一键注册");
            loginForgetPassWord.setVisibility(View.GONE);
            userNameLoginRegister.setText("");
            passWordLoginRegister.setText("");
            passWord2LoginRegister.setText("");
        }
    }
    private void initLoginOrRegister() {
        //点击"登录"
        llLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isLogin = true;
                initLoginRegisterpager();
            }
        });

        //点击"注册"
        llRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isLogin = false;
                initLoginRegisterpager();
            }
        });
    }

    //设置登陆或者注册的点击事件
    private void initLoginRegister() {
        btLoginRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mUsername = userNameLoginRegister.getText().toString();
                mPassword = passWordLoginRegister.getText().toString();
                mPassword2 = passWord2LoginRegister.getText().toString();
                if (!TextUtils.isEmpty(mUsername)&& !TextUtils.isEmpty(mPassword)) {
                    if (isLogin){
                        //Toast.makeText(getApplication(), "点了登陆", Toast.LENGTH_SHORT).show();
                        TestFragmentPre pre = new TestFragmentPre(LoginAndRegisterActivity.this);
                        pre.postLogin(mUsername,mPassword);
                    }else if ( !mPassword.equals(mPassword2)){
                        Toast.makeText(getApplication(), "点了注册，两次输入密码不一样,或者确认密码为空", Toast.LENGTH_SHORT).show();
                    }else {
                        //Toast.makeText(getApplication(), "点了注册，密码输入正确", Toast.LENGTH_SHORT).show();
                        TestFragmentPre pre = new TestFragmentPre(LoginAndRegisterActivity.this);
                        pre.postRegister(mUsername,mPassword);
                    }
                }else {
                    Toast.makeText(getApplication(), "用户名或者密码不能为空", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public void onHttpSuccess(int reqType, Message msg) {
        if(reqType == IHttpService.HTTP_REGISTER || reqType == IHttpService.HTTP_LOGIN){
            LoginBean loginBean = (LoginBean) msg.obj;
            if("error".equals(loginBean.getResponse())){
                Toast.makeText(this, loginBean.getError(), Toast.LENGTH_SHORT).show();
            }else if("register".equals(loginBean.getResponse())){
                Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
                login_register(loginBean);
            }else if("login".equals(loginBean.getResponse())){
                login_register(loginBean);
            }
        }
    }

    private void login_register(LoginBean loginBean) {
        SharedPreUtil.saveString(Global.mContext, Const.KEY_USERNAME,mUsername);
        SharedPreUtil.saveString(Global.mContext, Const.KEY_UID,loginBean.getUserInfo().getUserid());
        // 退出当前登录界面，回到主界面
        finish();
        //发送otto事件，通知界面舒心
        Message msg2 = new Message();
        msg2.what = IHttpService.HTTP_LOGIN;
        msg2.obj = loginBean;
        OttoBus.getDefault().post(msg2);
    }

    @Override
    public void onHttpError(int reqType, String error) {

    }
}
